﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Common;
using System.Data.SqlClient;

/// <summary>
/// Summary description for CtrlBDWorksIn
/// </summary>
public class CtrlBDWorksIn
{

    String strConnect;
    String sSql;
    SqlConnection connection;
    SqlCommand command;
    DataSet dataSet = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter();


	public CtrlBDWorksIn()
	{
        strConnect = ConfigurationManager.ConnectionStrings["conexion"].ConnectionString;
        connection = new SqlConnection(strConnect);
	}

    public void addRelation(int ipId, int ihId) 
    {

        connection.Open();

        sSql = "EXEC addRelation @ipId, @ihId";
        command = new SqlCommand(sSql, connection);
        SqlParameter p1 = new SqlParameter("@ipId", ipId);
        command.Parameters.Add(p1);
        SqlParameter p2 = new SqlParameter("@ihId", ihId);
        command.Parameters.Add(p2);
        command.ExecuteNonQuery();

        connection.Close();
    }

    public void deleteRelation(int ipId, int ihId)
    {

        connection.Open();
        sSql = "EXEC deleteRelation @ipId, @ihId";
        command = new SqlCommand(sSql, connection);
        SqlParameter p1 = new SqlParameter("@ipId", ipId);
        command.Parameters.Add(p1);
        SqlParameter p2 = new SqlParameter("@ihId", ihId);
        command.Parameters.Add(p2);
        command.ExecuteNonQuery();

        connection.Close();
    
    }

    public void consultRelation(int ipId) 
    {

        connection.Open();

        sSql = "EXEC consultRelation @ipId";
        command = new SqlCommand(sSql, connection);
        SqlParameter p1 = new SqlParameter("@ipId", ipId);
        command.Parameters.Add(p1);
        command.ExecuteNonQuery();

        connection.Close();

    }

    public int[] getRelations(int iId)
    {
        dataSet = new DataSet();

        connection.Open();

        sSql = "EXEC returnRelations @ipId";
        command = new SqlCommand(sSql, connection);
        SqlParameter p1 = new SqlParameter("@ipId", iId);
        command.Parameters.Add(p1);
        adapter = new SqlDataAdapter(command);
        adapter.Fill(dataSet, "Relations Project");

        connection.Close();


        int cuantity = dataSet.Tables[0].Rows.Count;

        int[] relations = new int[cuantity];


        for (int i = 0; i < cuantity; i++)
        {
            //Breakpoint para cuando el metodo de la base esté hecho.
            relations[i] = (int)(dataSet.Tables[0].Rows[i].ItemArray[0]);
        }



        return relations;
    }

}
